package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.service.AdminAdService;
import com.cskaoyan.market.service.AdminAdServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@WebServlet("/admin/ad/*")
public class AdminAdController extends HttpServlet {
    AdminAdService adminAdService=new AdminAdServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     String requestURI = req.getRequestURI();
     String op =requestURI.replace(req.getContextPath()+"/admin/ad/","");
     if("list".equals(op)){
         list(req,resp);
     }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp)  {
        String s= null;
        try {
            String page = req.getParameter("page");
            String limit = req.getParameter("limit");
            String order = req.getParameter("order");
            String sort = req.getParameter("sort");
            String name =req.getParameter("name");
            String content =req.getParameter("content");
            List<MarketAd> adList=adminAdService.list(page,limit,sort,order,name,content);
            Object o = ResponseUtil.okList(adList);
            s = JacksonUtil.getObjectMapper().writeValueAsString(o);
            resp.getWriter().println(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        String requestURI = req.getRequestURI();
        String op =requestURI.replace(req.getContextPath()+"/admin/ad/","");
        if("create".equals(op)){
            create(req,resp);
        }
        if("update".equals(op)){
            update(req,resp);
        }
        if("delete".equals(op)){
            delete(req,resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        String jsonStr = null;
        try {
            jsonStr = req.getReader().readLine();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            MarketAd marketAd = objectMapper.readValue(jsonStr, MarketAd.class);
            marketAd.setUpdateTime(LocalDateTime.now());
            Integer i=adminAdService.updateAd(marketAd);
            if(i>0){
                Object ok = ResponseUtil.ok();
                String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
                resp.getWriter().write(s);
                return;
            }
            Object fail=ResponseUtil.fail();
            String f=JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().write(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private void create(HttpServletRequest req, HttpServletResponse resp)  {
        String jsonStr = null;
        try {
            jsonStr = req.getReader().readLine();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            MarketAd marketAd = objectMapper.readValue(jsonStr, MarketAd.class);
            marketAd.setAddTime(LocalDateTime.now());
            marketAd.setUpdateTime(LocalDateTime.now());
            Integer i=adminAdService.createAd(marketAd);
            if(i>0){
                Object ok = ResponseUtil.ok();
                String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
                resp.getWriter().write(s);
                return;
            }
            Object fail=ResponseUtil.fail();
            String f=JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().write(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            String jsonStr =req.getReader().readLine();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            MarketAd marketAd = objectMapper.readValue(jsonStr, MarketAd.class);
            Integer i=adminAdService.deleteAd(marketAd);
            if(i>0){
                Object ok = ResponseUtil.ok();
                String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
                resp.getWriter().write(s);
                return;
            }
            Object fail=ResponseUtil.fail();
            String f=JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().write(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
